
#include "call_rates.hpp"
#include "consensus.hpp"
#include "easyrun.hpp"
#include "snp_cand.hpp"
#include "snp_call.hpp"
#include "expression.hpp"
#include "merge.hpp"
#include "view.hpp"
#include "info.hpp"


void main_usage()
{
    std::cerr << "Usage: comb <program> [OPTIONS] [ARGS]" << std::endl;
    std::cerr << std::endl;
    std::cerr << "where <program> is one of: " << std::endl;
    std::cerr << std::endl;
    std::cerr << "    easyrun           : do all snp calling steps (snpcand, callrates, snpcall)." << std::endl;
    std::cerr << std::endl;
    std::cerr << "    snpcall           : call snps." << std::endl;
    std::cerr << "    callrates         : generate call rates" << std::endl;
    std::cerr << "    snpcand           : generate snp candidates." << std::endl;
    std::cerr << "    view              : generate readable output from blob files" << std::endl;
    std::cerr << "    consensus         : generate consensus sequence" << std::endl;
    std::cerr << "    expression        : [COMING SOON] calculate expression" << std::endl;
    std::cerr << "    info              : [COMING SOON] information about blob files" << std::endl;
    std::cerr << std::endl;
    std::cerr << std::endl;
    std::cerr << "ADVANCED OPTIONS " << std::endl;
    std::cerr << std::endl;
    std::cerr << "    mapcount          : generate a list of map counts to be merged." << std::endl;
    std::cerr << "    mapcand           : generate snp candidates from map count files." << std::endl;
    std::cerr << "    merge             : combine file types from different runs." << std::endl;
    std::cerr << std::endl;
    std::cerr << "for more information: comb <program> help" << std::endl;
    std::cerr << "or see the manual online: http://code.google.com/p/comb/wiki/Manual" << std::endl;
    std::cerr << std::endl;
    std::cerr << "comb revision : " << HGVERSION << std::endl;
}

int main(int argc, char **argv)
{
    init_globals();

    bam_verbose = 0;

    // if no arguments or help is the first argument print usage and exit.
    if(argc < 2 || strcmp(argv[1], "help") == 0)
    {
        main_usage();
        exit(0);
    }

    // otherwise split on the first element, the command option to comb.
    std::string option(argv[1]);

    argv++;
    argc--;

    try
    {

        if      (option == "snpcand")       do_snpcand(   argc, argv);
        else if (option == "mapcount")      do_mapcount(  argc, argv);
        else if (option == "merge")         do_merge(     argc, argv);
        else if (option == "mapcand")       do_mapcand(   argc, argv);
        else if (option == "callrates")     do_callrates( argc, argv);
        else if (option == "snpcall")       do_snpcall(   argc, argv);
        else if (option == "easyrun")       do_easyrun(   argc, argv);
        else if (option == "expression")    do_expression(argc, argv);
        else if (option == "view")          do_view(      argc, argv);
        else if (option == "consensus")     do_consensus( argc, argv);
        else if (option == "info")          do_info(      argc, argv);
        else
        {
            main_usage();
            std::cerr << "Unknown option: " << argv[0] << std::endl;
            exit(1);
        }
    }
    catch ( std::bad_alloc &e )
    {
        std::cerr << "Out of memory: " << e.what() << std::endl;
    }
    catch( std::exception &e )
    {
        std::cerr << "Unrecognized exception encountered" << std::endl;
        std::cerr << e.what();
        std::cerr << std::endl << "Please contact the developers for help." << std::endl;
    }
}
